package apptoko.com;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import apptoko.com.adapter.ErrorHandler;
import apptoko.com.background.BackgroundTasks;
import apptoko.com.event.Event.ChangePasswordFailEvent;
import apptoko.com.event.Event.ChangePasswordSuccessEvent;
import apptoko.com.event.Event.RequestFailEvent;
import apptoko.com.network.ProgressDialogManager;
import apptoko.com.util.Utils;
import apptoko.com.validator.Form;
import apptoko.com.validator.Validate;
import apptoko.com.validator.validate.ConfirmValidate;
import apptoko.com.validator.validator.MinimumPasswordValidator;
import apptoko.com.validator.validator.MinimumUsernameValidator;
import apptoko.com.validator.validator.NotEmptyValidator;

import apptoko.com.R;

import de.greenrobot.event.EventBus;

public class ChangePasswordActivity extends Activity {
	
	private EditText editOldPass;
	private EditText editNewPass;
	private EditText editConfirmPass;
	private Button btnConfirm;
	private String accessToken = "";
	private BackgroundTasks bgTask;
	private Form form;
	private ProgressDialog pDialog;
	private int uid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_pass);
		setTitle(R.string.change_password);
		accessToken = Utils.getStaticAccessToken(this);
		editOldPass = (EditText) findViewById(R.id.edt_old_password);
		editNewPass = (EditText) findViewById(R.id.edt_new_password);
		editConfirmPass = (EditText) findViewById(R.id.edt_confirm_password);
		btnConfirm = (Button) findViewById(R.id.btn_confirm_change_pwd);
		bgTask = new BackgroundTasks(this);
		form = new Form();
		if(getIntent()!=null&&getIntent().getExtras()!=null&&getIntent().getExtras().containsKey("uid")){
			uid=getIntent().getExtras().getInt("uid");
		}
		Validate oldPassValidate = new Validate(editOldPass);
		oldPassValidate.addValidator(new NotEmptyValidator(this));
		oldPassValidate.addValidator(new MinimumPasswordValidator(this));
		Validate newPassValidate = new Validate(editNewPass);
		newPassValidate.addValidator(new NotEmptyValidator(this));
		newPassValidate.addValidator(new MinimumPasswordValidator(this));
		Validate confirmValidate = new Validate(editConfirmPass);
		confirmValidate.addValidator(new NotEmptyValidator(this));
		confirmValidate.addValidator(new MinimumPasswordValidator(this));
		ConfirmValidate confirmFields = new ConfirmValidate(editNewPass, editConfirmPass);
		form.addValidates(oldPassValidate);
		form.addValidates(newPassValidate);
		form.addValidates(confirmValidate);
		form.addValidates(confirmFields);
		
		btnConfirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(form.validate()){
					pDialog = ProgressDialogManager.showProgressDialog(ChangePasswordActivity.this, R.string.loading);
//					bgTask.changePassword(accessToken, editOldPass.getText().toString(), editNewPass.getText().toString());
					bgTask.changePassword(accessToken, (int)uid, editOldPass.getText().toString(), editNewPass.getText().toString());
				}
			}
		});
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		EventBus.getDefault().register(this);
	}
	
	public void onEventMainThread(ChangePasswordSuccessEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		Toast.makeText(this, event.getMessage(), Toast.LENGTH_SHORT).show();
		finish();
	}
	
	public void onEventMainThread(ChangePasswordFailEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		Toast.makeText(this, event.getMessage(), Toast.LENGTH_SHORT).show();
	}
	
	public void onEventMainThread(RequestFailEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		ErrorHandler.getInstance().setContext(this).handleError(event.getErrorCode());
	}

}
